package org.link.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.link.bean.Account;
import org.link.util.HibernateUtil;

/**
 * 
 * project_name:hibernatedemo
 * package_name:org.link.dao
 * file_name:AccountDao.java
 * @author Link
 * create_date:2015年12月10日 下午5:11:49
 * modified_date:
 * description:hibernate dao层
 *
 */
public class AccountDao implements IAccountDao {

	 
	/**
	 * 添加账户
	 */
	public void addAccount(Account account) {
		Session session = HibernateUtil.getSession();
		//获取事务
		Transaction tx = session.beginTransaction();
		session.save(account);//向数据库录入数据
		tx.commit();//提交事务
		session.close();
		
	}

	/**
	 * 查询账户
	 */
	public Account getAccountById(String id) {
		Session session = HibernateUtil.getSession();
		//获取事务
		Transaction tx = session.beginTransaction();
		Account account = (Account) session.load(Account.class, id);
		tx.commit();//提交事务
		//session.close();
		return account;
	}

	/**
	 * 删除用户
	 */
	public void deleteAccount(Account account) {
		Session session = HibernateUtil.getSession();
		//获取事务
		Transaction tx = session.beginTransaction();
		session.delete(account);
		tx.commit();//提交事务
		session.close();
	}

	/**
	 * 更新用户信息
	 */
	public void updateAccountById(String payId, String gatherId, String money) {
		Session session = HibernateUtil.getSession();
		Account payAccount = (Account) session.load(Account.class, payId);
		Account gatherAccount = (Account) session.load(Account.class, gatherId);
		payAccount.setMoney(payAccount.getMoney()-Double.parseDouble(money));
		gatherAccount.setMoney(gatherAccount.getMoney()+Double.parseDouble(money));
		session.update(payAccount);
		session.update(gatherAccount);
		//获取事务
		Transaction tx = session.beginTransaction();
		tx.commit();//提交事务
		session.close();
	}

}
